from pyecharts.charts.chart import Base
from pyecharts.charts.mixins import ChartMixin
from pyecharts.charts import Tab
from pyecharts.charts.composite_charts.page import Page
from pyecharts import options as opts
import os
from os.path import isdir, join
import uuid
from typing import *
from utils.error import DrawError
from config import ROOT_DIR


class BaseDraw:
    def __init__(self, name: str = None, drag: bool = False):
        self.name: str = name if name else uuid.uuid4().__str__()
        self.tab = Page(page_title=self.name, layout=Page.DraggablePageLayout if drag else Page.SimplePageLayout)
        self.charts: List[ChartMixin] = []

    def draw(self, chart: ChartMixin) -> None:
        self.charts.append(chart)
        self.tab.add(chart)

    def dump(self, path: str):
        path = join(ROOT_DIR, path)
        if not isdir(path):
            raise DrawError(f'path not a directory: {path}')

        self.tab.render(join(path, f'{self.name}.html'))
